package cn.iocoder.yudao.module.infra.utils;

import cn.iocoder.yudao.framework.common.GameServerUtil;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.DefaultThreadFactory;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class EngineExecutorUtils {

    // Netty事件循环组(用于网络I/O操作)
    public static EventLoopGroup newNioEventLoopGroup(int nThreads, String poolName) {
        log.info("创建线程池-newNioEventLoopGroup, poolName = {}, nThreads = {}", poolName, nThreads);
        return (GameServerUtil.isLinuxPlatform() && Epoll.isAvailable())
                ? new EpollEventLoopGroup(nThreads, new DefaultThreadFactory(poolName))
                : new NioEventLoopGroup(nThreads, new DefaultThreadFactory(poolName));
    }

    // 业务线程池(专门用于处理第三方接口调用)
    public static ExecutorService newThreadPoolExecutor(int corePoolSize, String poolName) {
        log.info("创建线程池-newThreadPoolExecutor, poolName = {}, corePoolSize = {}", poolName, corePoolSize);
        return Executors.newFixedThreadPool(corePoolSize, new DefaultThreadFactory(poolName));

    }

    public static EventExecutorGroup newDefaultEventExecutorGroup(int nThreads, String poolName) {
        log.info("创建线程池-newDefaultEventExecutorGroup, poolName = {}, nThreads = {}", poolName, nThreads);
        return new DefaultEventExecutorGroup(nThreads, new DefaultThreadFactory(poolName));
    }

}